package homework;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * 服务器端
 */
public class Server {
    private ServerSocket ServerSocket;

    public Server() {
        try {
            System.out.println("正在创建服务器");
            ServerSocket = new ServerSocket(8090);
            System.out.println("服务器已创建");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {

        Socket socket = null;
        try {
            System.out.println("等待客户端连接");
            socket = ServerSocket.accept();
            System.out.println("一个客户端已连接");
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            while (true) {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader bis = new BufferedReader(isr);
                String massage;
                while ((massage = bis.readLine()) != null) {
                    System.out.println(massage);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        Server server = new Server();
        server.start();

    }
}
